import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author lys
 * 登录界面
 */

public class Login extends JFrame {
    public static String loginUser;
    private static Login instance;

    private JLabel jLabel1 = new JLabel("登录");
    private JLabel jLabel2 = new JLabel("用户名:");
    private JLabel jLabel3 = new JLabel(" 密码 :");

    private JTextArea jTextArea = new JTextArea();
    private JPasswordField jPasswordField = new JPasswordField();

    private JPanel jPanel1 = new JPanel();
    private JPanel jPanel2 = new JPanel();

    //字体
    private Font font1 = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,40);
    private Font font3 = new Font("宋体",Font.BOLD,30);

    private JButton jButton1 = new JButton("登录");
    private JButton jButton2 = new JButton("注册");
    private Login() {
        Icon i = new ImageIcon(Login.class.getResource("img/2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 800, 800);

        jLabel1.setFont(font1);
        jLabel1.setBounds(310, 50, 300, 80);
        jLabel1.setForeground(Color.white);

        //用户名
        jLabel2.setFont(font2);
        jLabel2.setBounds(150, 270, 200, 50);
        jLabel2.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea.setPreferredSize(new Dimension(350,50));
        jTextArea.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea.setFont(font2);
        jTextArea.setForeground(Color.black);
        jPanel1.setBounds(0,240,800,60);
        jPanel1.add(jLabel2);
        jPanel1.add(jTextArea);

        //密码
        jLabel3.setFont(font2);
        jLabel3.setBounds(150, 270, 200, 50);
        jLabel3.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jPasswordField.setPreferredSize(new Dimension(350,50));
        jPasswordField.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jPasswordField.setFont(font2);
        jPasswordField.setForeground(Color.black);
        jPanel2.setBounds(0,400,800,60);
        jPanel2.add(jLabel3);
        jPanel2.add(jPasswordField);

        jButton1.setBounds(250,600,300,60);
        jButton1.setFont(font2);
//        jButton1.setForeground(Color.white);
        jButton2.setBounds(620,650,100,50);
//        jButton2.setForeground(Color.white);
        jButton2.setFont(font3);

        jPanel1.setOpaque(false);
        jPanel2.setOpaque(false);
        jTextArea.setOpaque(false);
        jPasswordField.setOpaque(false);

        add(jLabel1);
        add(jPanel1);
        add(jPanel2);
        add(jButton1);
        add(jButton2);
        add(Label);

        setVisible(true);
        setSize(800,800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String usr = jTextArea.getText();
                String pwd = jPasswordField.getText();
                login(usr, pwd);
            }
        });
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Login.getInstance().setVisible(false);
                Register.getInstance().setVisible(true);
            }
        });

        jButton1.setFocusPainted(false);
        jButton2.setFocusPainted(false);
    }

    public static Login getInstance() {
        if(instance==null) instance = new Login();
        return instance;
    }

//    public static void main(String[] args) {
//        Login.getInstance();
//    }

    /**
     * 将登录信息与数据库进行核对
     * @param username
     * @param password
     */
    public void login(String username, String password){
        ResultSet rs = null;
        PreparedStatement preSql;
        String sqlStr;
        Connection con = DatabaseConnection.getInstance().getCon();
        if(password.equals(Admin.getInstance().getPassword())&&username.equals(Admin.getInstance().getUsername())){
            Login.loginUser = username;
            Login.getInstance().setVisible(false);
            AdminInterface.getInstance().admin.setVisible(true);
            return;
        }
        if(!password.equals(Admin.getInstance().getPassword())&&username.equals(Admin.getInstance().getUsername())){
            JOptionPane.showMessageDialog(null, "密码不正确");
            return;
        }
        try{
            sqlStr = "select * from user where username = ?";
            preSql = con.prepareStatement(sqlStr);
            preSql.setString(1,username);
            rs = preSql.executeQuery();
            if(rs.next()){
                if(!rs.getString("password").equals(password)){
                    JOptionPane.showMessageDialog(null, "密码不正确");
                    return;
                }
                if(rs.getBoolean("isClosed")){
                    JOptionPane.showMessageDialog(null, "您的账号已被管理员封禁");
                    return;
                }
            }
            else {
                JOptionPane.showMessageDialog(null, "请先注册一个账号");
                return;
            }
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        Login.loginUser = username;
        Login.getInstance().setVisible(false);
        UserInterface.getInstance().user.setVisible(true);
    }
}